<template>
  <div class="m-export-excel-container">
    <el-button type="primary" size="small" icon="el-icon-document" @click="handleExport">导出</el-button>
  </div>
</template>

<script>

export default {
  name: 'Index',

  props: {
    action: {
      type: String,
      required: true
    },
    extraData: {
      type: Object,
      default() {
        return null
      }
    },
    filename: {
      type: String,
      default: ''
    },
    sheetName: {
      type: String,
      default: ''
    },
    onBeforeExport: {
      type: Function,
      default() {
        return true
      }
    }
  },

  methods: {
    handleExport() {
      if (this.onBeforeExport.call({ extraData: this.extraData })) {
        let url = this.action + '?'
        if (this.extraData != null) {
          for (const o in this.extraData) {
            if (this.extraData[o] !== null) {
              url = `${url}${o}=${this.extraData[o]}&`
            }
          }
          url = `${url}filename=${this.filename}&sheetName=${this.sheetName}`
        } else {
          url = `${url}?filename=${this.filename}&sheetName=${this.sheetName}`
        }
        window.location.href = url
      }
    }
  }

}
</script>

<style scoped lang="scss">
.m-export-excel-container {
  width: 100%;
  text-align: end;
}
</style>
